/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import { FileSystemSnapshotHierarchyVisitor, SnapshotVisitResult } from '../generator/filesystem-snapshot-visitor.js';

import { BasicFileSnapshot } from './filesystem-snapshot.js';

/**
 * 文件夹快照
 *
 * @since 2022/9/1
 */
export class DirectorySnapshot extends BasicFileSnapshot {
  children: BasicFileSnapshot[] = [];

  accept(visitor: FileSystemSnapshotHierarchyVisitor): SnapshotVisitResult {
    const result = visitor.visitEntry(this);
    switch (result) {
      case SnapshotVisitResult.CONTINUE:
        visitor.enterDirectory(this);
        for (const child of this.children) {
          child.accept(visitor);
        }
        visitor.leaveDirectory(this);
        return SnapshotVisitResult.CONTINUE;
      case SnapshotVisitResult.SKIP_SUBTREE:
        return SnapshotVisitResult.CONTINUE;
      case SnapshotVisitResult.TERMINATE:
        return SnapshotVisitResult.TERMINATE;
      default:
        return SnapshotVisitResult.CONTINUE;
    }
  }

  *walk(): IterableIterator<BasicFileSnapshot> {
    yield this;
    for (const subDir of this.children) {
      yield* subDir.walk();
    }
  }

  equals(other: BasicFileSnapshot): boolean {
    if (!(other instanceof DirectorySnapshot)) {
      return false;
    }
    return super.isHashEquals(other);
  }
}
